package com.asset.dispoal.business.casedispose.service.command;

import com.asset.dispoal.business.casedispose.domain.db.mapper.CasedisposeMapper;
import com.asset.dispoal.business.casedispose.domain.dto.CaseDisposeFormulaDTO;
import com.asset.dispoal.business.casedispose.domain.dto.CaseDisposeFormulaDetailDTO;
import com.asset.dispoal.business.casedispose.dto.CaseDisposeFormulaRequest;
import com.asset.dispoal.business.casedispose.dto.CaseDisposeFormulaResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.date.DateTimeUtil;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.springboot.protocol.Result;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class CaseDisposeFormulaCommand implements BaseCommand<CaseDisposeFormulaRequest, Result<CaseDisposeFormulaResponse>> {

    @Autowired
    private CasedisposeMapper casedisposeMapper;

    @Override
    public Result<CaseDisposeFormulaResponse> execute(CaseDisposeFormulaRequest request) {
        CaseDisposeFormulaResponse response = new CaseDisposeFormulaResponse();
        BigDecimal result = BigDecimal.ZERO;
        if ("固定".equals(request.getJudgmentType())) {
            BigDecimal calculate = calculate(request.getJudgmentCalculationTime(), null, request.getBaseNumber(), request.getJudgmentRate(), request.getJudgmentDay());
            result = result.add(calculate == null ? BigDecimal.ZERO : calculate);
        } else {
            CaseDisposeFormulaDTO caseDisposeFormulaDTO = casedisposeMapper.getFormulaDataById(request.getJudgmentRate());
            List<Map<String, BigDecimal>> list = new ArrayList<>(caseDisposeFormulaDTO.getDetailList().size());
            for (CaseDisposeFormulaDetailDTO item : caseDisposeFormulaDTO.getDetailList()) {
                BigDecimal calculate = calculate(request.getJudgmentCalculationTime(), item, request.getBaseNumber(), null, null);
                result = result.add(calculate == null ? BigDecimal.ZERO : calculate);
                //天数
                Map<String, BigDecimal> map = new HashMap<>(1);
                map.put(item.getBigRate() + ":比例", calculate);
                list.add(map);
            }
            response.setRateDetailList(list);
        }
        response.setJudgmentMoney(result);
        return Result.ok(response);
    }

    private BigDecimal calculate(Date judgmentCalculationTime, CaseDisposeFormulaDetailDTO item, BigDecimal baseNumber, String judgmentRate, Integer judgmentDay) {
        if (item != null) {
            //如果计算的时间大于开始时间则进行处理，如果不大于开始时间则不允许进行运算
            if (judgmentCalculationTime.after(item.getStartTime())) {
                //如果计算时间大于结束时间
                if (judgmentCalculationTime.after(item.getEndTime())) {
                    judgmentCalculationTime = item.getEndTime();
                }
                //取得范围天数
                long between = DateTimeUtil.getLocalDateDiffDay(DateTimeUtil.dateToLocalDate(item.getStartTime()), DateTimeUtil.dateToLocalDate(judgmentCalculationTime));
                return baseNumber.multiply(BigDecimal.valueOf(between)).multiply(item.getBigRate()).setScale(2, RoundingMode.HALF_UP);
            }
            return BigDecimal.ZERO;
        } else {
            long between = DateTimeUtil.getLocalDateDiffDay(DateTimeUtil.dateToLocalDate(judgmentCalculationTime), LocalDate.now());
            return baseNumber.multiply(BigDecimal.valueOf(between)).multiply(new BigDecimal(judgmentRate)).divide(judgmentDay == null ? BigDecimal.ONE : BigDecimal.valueOf(judgmentDay), 2, RoundingMode.HALF_UP);
        }
    }
}
